<?php
/**
 * Created by PhpStorm.
 * User: 王峥宇
 * Date: 2019-04-24
 * Time: 13:33
 */

namespace app\admin\controller;

use app\admin\model\Indent as IndentModel;
use app\admin\model\Zpaytype;
use app\admin\model\Yuyue as YuyueModel;
use app\services\MessageServices;
use think\Controller;
use think\Request;

class Indent extends Controller
{
    //显示发单页面
    public function index()
    {
        $indentNew = IndentModel::getIndentIndex();
        $page = $indentNew->render();
        $this->assign('indent', $indentNew);
        $this->assign('page', $page);
        return $this->fetch();
    }
    //显示添加单
    public function insert()
    {
        if(request()->isAjax()) {
            $inserts = IndentModel::getInsert();
            if($inserts==0){
                return 0;
            }else{
                $this->delivery($inserts);
                return 1;
            }
            return $inserts;
        }
        $ztype = Zpaytype::ztype();
        $this->assign('ztype',$ztype);
        return $this->fetch();
    }
    //寻找符合条件的抢单用户
    public function delivery($tid)
    {
        $indent=IndentModel::where('tid',$tid)->find();//订单信息
        $yuyue=YuyueModel::alias('a')
            ->join('jc_user b','a.uid=b.uid','left')
            ->where('a.zid',$indent['zid'])
            ->where('b.u_money','>',$indent['t_money'])
            ->where('yid','>',$indent['sort'])
            ->order('yid asc')
            ->field('a.*,b.u_money')
            ->find();
        if(!empty($yuyue)){//下面还有排队等单的人 就执行
            $re=YuyueModel::where('yid',$yuyue['yid'])//订单传递给下一个人
            ->update([
                'tid'=>$tid,
                'y_state'=>2
            ]);
            $re2=IndentModel::where('tid',$tid)->update(['sort'=>$yuyue['yid']]);
            if($re&&$re2){//向下一个人推送消息
                //TODO ADFSA
                $re=MessageServices::pushmessage($yuyue['uid'],$tid,$yuyue['yid']);
             	if($re=="ok"){
                	return true;
                }else{
                	return false;
                }
            }
        }
    }
    //显示修改单面
    public function update(){
        if(request()->isAjax()) {
            $update = IndentModel::getUpdate();
            return $update;
        }else {
            $updateOld = IndentModel::getUpdate();
            $this->assign('updateOld',$updateOld);
            $ztype = Zpaytype::ztype();
            $this->assign('ztype',$ztype);
            return $this->fetch();
        }
    }
    //匹配成功
    public function succindent(){
        $indentNew = IndentModel::getSuccIn();
        $page = $indentNew->render();
        $this->assign('indent', $indentNew);
        $this->assign('page', $page);
        return $this->fetch();
    }
    //匹配成功->打款成功
    public function updatedk(){
        $dk = IndentModel::getSuccIndk();
        return $dk;
    }
    //打款成功
    public function succdk(){
        $indentNew = IndentModel::getSuccInMake();
        $page = $indentNew->render();
        $this->assign('indent', $indentNew);
        $this->assign('page', $page);
        return $this->fetch();
    }
    //打款成功->交易成功
    public function updatepay(){
        $dk = IndentModel::getSuccInpay();
        return $dk;
    }
    //交易成功
    public function succpay(){
        $indentNew = IndentModel::getSuccPay();
        $page = $indentNew->render();
        $this->assign('indent', $indentNew);
        $this->assign('page', $page);
        return $this->fetch();
    }
    public function uplode()
    {
        $gai = IndentModel::xiugai();
        $this->redirect('indent/succdk');
    }
}